package com.naizhuai.ui.task.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.util.NaizhuAILog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author：lichenxi
 * @date 2018/4/19 19
 * email：525603977@qq.com
 * Fighting
 */
public class QuestionPlanAdapter extends RecyclerView.Adapter<QuestionPlanAdapter.Holder> {

    private List<String> mImgList;

    public QuestionPlanAdapter() {
        mImgList = new ArrayList<String>();
    }

    public List<String> getImgList() {
        return mImgList;
    }

    public void add(String imgurl) {
        int index=mImgList.indexOf(imgurl);
        if (index!=-1){
            mImgList.set(index, imgurl);
            notifyItemChanged(index);
        }else{
            index=~index;
            mImgList.add(index, imgurl);
            notifyItemInserted(index);
        }

    }
    private void delete(int indexposition) {
        mImgList.remove(indexposition);
        notifyItemRemoved(indexposition);
    }
    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_question_gallery, parent, false);
        return new Holder(root);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        Glide.with(holder.itemView.getContext()).load(mImgList.get(position))
                 .into(holder.imageBackground);
        holder.imageBackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaizhuAILog.e("imageBackground");
            }
        });
        holder.imageDelete.setOnClickListener(v -> delete(holder.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return mImgList != null ? mImgList.size() : 0;
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_background)
        ImageView imageBackground;
        @BindView(R.id.image_delete)
        ImageView imageDelete;
        Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
